import { ref } from 'vue'
import { Direction } from '../constants'

const allowedNewDirectionsByOldDirection: Record<Direction, Direction[]> = {
  [Direction.LEFT]: [Direction.UP, Direction.DOWN],
  [Direction.RIGHT]: [Direction.UP, Direction.DOWN],
  [Direction.UP]: [Direction.LEFT, Direction.RIGHT],
  [Direction.DOWN]: [Direction.LEFT, Direction.RIGHT]
}
export const defaultDirection = Direction.RIGHT

export const useDirection = (startDirection: Direction = defaultDirection) => {
  const direction = ref(startDirection)

  const setDirection = (newDirection: Direction) => {
    const allowedDirections =
      allowedNewDirectionsByOldDirection[direction.value]
    if (!allowedDirections.includes(newDirection)) return
    direction.value = newDirection
  }
  return [direction, setDirection] as const
}
